using System.Data;
using MySqlConnector;

namespace Front._1127;

public partial class BookForm : Form
{
    
    public BookForm()
    {
        InitializeComponent();
    }

    private void BookForm_Load(object sender, EventArgs e)
    {

        GetAllBooks();
        GetAllBookCategory();
    }

    private void GetAllBookCategory()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=liushuqi123456.");
        connection.Open();
        string sql = "SELECT distinct book_category FROM book_info;";
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        
        adapter.Fill(dataSet, "book_category");
        
        DataTable? dataTable = dataSet.Tables["book_category"];
        DataRow row =dataTable.NewRow();
        row[0] = "全部";
        dataTable.Rows.InsertAt(row, 0);
        this.comboBox1.DataSource = dataTable;
        this.comboBox1.DisplayMember = dataTable.Columns[0].ColumnName;
    }

    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=liushuqi123456.");
        connection.Open();
        string sql = "SELECT book_id,isbn,book_name,author FROM book_info;";
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        
        adapter.Fill(dataSet, "book_info");
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }

    private void button2_Click(object sender, EventArgs e)
    {
        AddBookForm addbookForm = new AddBookForm();
        addbookForm.ShowDialog();
    }
}